<template>
  <div class="search_history">
    <van-cell title="搜索历史" class="vancellHistory">
      <template v-if="isDeleteShow">
        <span @click="$emit('clear-search-histories', [])">全部删除</span>
        <span @click="isDeleteShow = false">完成</span>
      </template>
      <van-icon v-else name="delete" @click="isDeleteShow = true"></van-icon>
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="onSearchItem(item, index)"
    >
      <van-icon
        name="close"
        v-show="isDeleteShow"
      ></van-icon>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'searchHistory',
  data() {
    return { isDeleteShow: false }
  },
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  methods: {
    onSearchItem(item, index) {
      if (this.isDeleteShow) {
        // 删除状态下，删除历史记录
        this.searchHistories.splice(index, 1)
      } else {
        // 非删除状态 直接进入搜索状态
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less">
.search_history {
  .vancellHistory {
    span {
      margin-left: 5px;
    }
  }
}
</style>
